package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.MyDesktop;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.ButtonModelEnum;
import com.hy.mydesktop.client.component.meta.MenuItemModelEnum;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：汤莉</li>
 * <li>设计日期：2010-8-30；时间：上午10:55:33</li>
 * <li>类型名称：MenuItemFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class MenuItemFactory {

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建菜单项</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午10:56:14</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaModel
	 * @return
	 */
	public static MenuItem createMenuItem(GxtComponentMetaNodeModel gxtComponentMetaModel){
		return configureMenuItem(gxtComponentMetaModel);
		
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：配置菜单项属性</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午10:57:01</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static MenuItem configureMenuItem(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		final MenuItem item=createDafaultMenuItem();
		AbstractComponetFactory.configureAbstractComponet(item, gxtComponentMetaNodeModel);

		
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.CHILDREN.name().toLowerCase())!=null){
			//添加子控件
			 List<GxtComponentMetaNodeModel> list =(List<GxtComponentMetaNodeModel>)gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.CHILDREN.name().toLowerCase());
			 Menu menu=new Menu();
			 for(int i=0;i<list.size();i++)
			 {
				 MenuItem subItem=MenuItemFactory.createMenuItem(list.get(i));
				 menu.add(subItem);
			 }	
			 item.setSubMenu(menu);
		}
		
		
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT1.name().toLowerCase())!=null){
			//设置显示文本
			item.setText((String)gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT1.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT.name().toLowerCase())!=null){
			//设置显示文本
			item.setText((String)gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.ICON1.name().toLowerCase())!=null){
			//设置图标
			item.setIcon((AbstractImagePrototype)gxtComponentMetaNodeModel.get(MenuItemModelEnum.ICON1.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.MENU1.name().toLowerCase())!=null){
			//设置子菜单项
			
			item.setSubMenu((Menu)gxtComponentMetaNodeModel.get(MenuItemModelEnum.MENU1.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.SUBMENU.name().toLowerCase())!=null){
			//设置三级菜单
			List<BaseTreeModel> list=gxtComponentMetaNodeModel.get(MenuItemModelEnum.SUBMENU.name().toLowerCase());
			Menu menu=new Menu();
			for(BaseTreeModel z:list)
			{
				menu.add(new MenuItem(z.get("name").toString()));	
			}
			item.setSubMenu(menu);
			gxtComponentMetaNodeModel.set("submenu", null);
		}
		
		
		if(gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
			
			final Map<String,EventStructureMetaModel> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	
		                break;
		            case 5://Integer.valueOf("0101")
				    	final String componentId=item.getData(ComponentMetaDataConstants.COMPONENT_ID);

		            	item.addSelectionListener(new SelectionListener<MenuEvent>() {

							@Override
							public void componentSelected(MenuEvent ce) {
								//itemSelected(desktop,ce);
						    	  //TODO 如何传递数据伴随Appeve事件的数据？
								 Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
						    	  data.put("windowContainer", item);
						    	  EventStructureMetaModel eventstructuremetamodel = 
										 (EventStructureMetaModel)data.get("eventstructuremetamodel");
						    	  System.out.println("111111111111111StartMenu的MuneItem触发AppEvents.DESKTOP_APPLICATION_INIT事件:"
						    			  +" 控制器的Controller Id 是"+eventstructuremetamodel.getControllerId()
						    			 );
						    	  //窗口未被加载并初始化
						    	  //把窗口控制器或者窗口控件的Id，加入MenuItem
						    	  item.setData(MetaDataConstants.CONTROLLER_ID, "1_1_windowController_PositonType");
						    	  item.setData(ComponentMetaDataConstants.COMPONENT_ID, "01_01_windowGxtComponent_PositonType");
						    	  //窗口已经被加载并初始化，则使窗口前端显示
						    	  Map<String, BaseController> gxtControllerMap =Registry.get(MyDesktop.GXTCONTROLLER_MAP);
							      BaseController tempbBaseController = gxtControllerMap.get(item.getData(MetaDataConstants.CONTROLLER_ID).toString());
							      Window w =(Window)tempbBaseController.getPresenter().getDisplay().getComponent();
							  	   
							  		Desktop desktop = Registry.get(MyDesktop.DESKTOP);
							  	    /*if (!desktop.getWindows().contains(w)) {
							  	      desktop.addWindow(w);
							  			w.show();
	
							  	    }*/
							  	    if (w != null && !w.isVisible()) {
							  	      w.show();
							  	    } else {
							  	      w.toFront();
							  	    }							  	    							  	  									
									
							    	  MyLoggerUtil.printComponentDebugForEventTrigger(getClass(), AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT.toString());
							    	  MyLoggerUtil.printDebug(getClass(),"控件ID:"+componentId+" ,发出的事件模型EventStructureMetaModel的结构为【rootControllerId:"+eventstructuremetamodel.getRootControllerId()+";ControllerId:"+eventstructuremetamodel.getControllerId()+"】【】【】");

						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT,data);
							}
						});
		                break;
	           		           
		            default:
		                break;
		        }
			}
			/*List<String> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for (String code : appEventCodes) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
						      @Override
						      public void componentSelected(ComponentEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  //TODO 如何传递数据伴随Appeve事件的数据？
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
								 
					    }};
					    *//**
						 * 桌面快捷方式的监听器
						 *//*
					    shortcut.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}		*/
		
		}
		
		return item;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建默认的菜单项</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-31；时间：下午下午02:33:48</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static MenuItem createDafaultMenuItem(){
		MenuItem item=new MenuItem();
		item.setText("查询历史");
		
		return item;
	}
	
	public static MenuItem createItemOfAssociateToWindow(
			ItemOfAssociateToWindow gxtComponentMetaNodeModel) {
		final MenuItem item=createDafaultMenuItem();
		//AbstractComponetFactory.configureAbstractComponet(item, gxtComponentMetaNodeModel);

		
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			//添加子控件
			 List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
			 Menu menu=new Menu();
			 for(int i=0;i<list.size();i++)
			 {
				 MenuItem subItem=MenuItemFactory.createItemOfAssociateToWindow(list.get(i));
				 menu.add(subItem);
			 }	
			 item.setSubMenu(menu);
		}

		//进行个性化配置
		
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT1.name().toLowerCase())!=null){
			//设置显示文本
			item.setText((String)gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT1.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT.name().toLowerCase())!=null){
			//设置显示文本
			item.setText((String)gxtComponentMetaNodeModel.get(MenuItemModelEnum.TEXT.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.ICON1.name().toLowerCase())!=null){
			//设置图标
			item.setIcon((AbstractImagePrototype)gxtComponentMetaNodeModel.get(MenuItemModelEnum.ICON1.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.MENU1.name().toLowerCase())!=null){
			//设置子菜单项
			
			item.setSubMenu((Menu)gxtComponentMetaNodeModel.get(MenuItemModelEnum.MENU1.name().toLowerCase()));
		}
		/*if(gxtComponentMetaNodeModel.get(MenuItemModelEnum.SUBMENU.name().toLowerCase())!=null){
			//设置三级菜单
			List<BaseTreeModel> list=gxtComponentMetaNodeModel.get(MenuItemModelEnum.SUBMENU.name().toLowerCase());
			Menu menu=new Menu();
			for(BaseTreeModel z:list)
			{
				menu.add(new MenuItem(z.get("name").toString()));	
			}
			item.setSubMenu(menu);
			gxtComponentMetaNodeModel.set("submenu", null);
		}*/
		
		
		
		return item;
		
	}


}
